import { ref, reactive, onMounted, computed, nextTick } from 'vue';
import { emitter } from "@/utils/mitt";
import { ElMessage, FormInstance, RowAlign } from "element-plus";
import {
  ProFixPriceGetDeciDetail,
  ProFixPriceGetInsureList,
  ProFixPriceInsureDeci
} from "@/api/stu/InsuranceData";
import { type PaginationProps } from "@pureadmin/table";
import qs from 'qs';
import { useRoute } from "vue-router";
export function useMyInsurance() {
  const route = useRoute();
  const columns = [
    {
      label: "序号",
      type: "index",
      minWidth: 100
    }, {
      label: "保单号",
      prop: "piNumber",
    },
    {
      label: "投保人",
      prop: "piApplicant",
    },
    {
      label: "保险产品名称",
      prop: "ppName",
    },
    {
      label: "保险类型",
      prop: "pType",
    }, {
      label: "操作",
      fixed: "right",
      width: 150,
      slot: "operation"
    }

  ];
  const dataList = ref([]);
  const loading = ref(true);
  const btnloading = ref<boolean>(false);
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 30,
    currentPage: 1,
    pageSizes: [10, 30, 50, 100],
    background: true
  });
  const RowData = ref();
  const carmodel = ref(false);
  const car_insurance_item = ref<any>({});
  async function SendData(row) {
    const res = await ProFixPriceGetDeciDetail(qs.stringify({ PIId: row.piId }))
    if (res.success) {
      carmodel.value = true;
      RowData.value = row;
      nextTick(() => {
        car_insurance_item.value = res.data;
      })
    } else {
      ElMessage.error(res.msg)
    }
  }

  //  关闭-拒绝投保决策
  async function close_from() {
    carmodel.value = false;
    RowData.value = ""
    car_insurance_item.pINumber = ""
    car_insurance_item.ppName = ""
    car_insurance_item.piApplicant = ""
    car_insurance_item.applicantAge = ""
    car_insurance_item.applicantSex = ""
    car_insurance_item.carType = ""
    car_insurance_item.carUse = ""
    car_insurance_item.seatTonnage = ""
    car_insurance_item.carUseYear = ""
    car_insurance_item.buyCarPrice = ""
    car_insurance_item.count = ""
    car_insurance_item.applyTime = ""
    car_insurance_item.foundAmount = ""
    car_insurance_item.ncdCoef = ""
    car_insurance_item.actCoef = ""
    car_insurance_item.finalPrice = ""
  }

  // 确定投保决策
  async function stubim_from(row) {
    const data = {
      "taskId": route.query.taskId,
      "planId": route.query.planId,
      PIId: RowData.value.piId,
      Result: row === 0 ? '拒绝' : '同意',
      piApplicant: RowData.value.piApplicant,
      ppName: RowData.value.ppName
    }
    btnloading.value = true;
    const res = await ProFixPriceInsureDeci(data);
    if (res.success) {
      ElMessage.success(res.msg)
      close_from()
      GetListData()
      btnloading.value = false;
    } else {
      ElMessage.error(res.msg)
      setTimeout(() => { btnloading.value = false; }, 1500)
    }
  }

  function handleSizeChange() { GetListData() }
  function handleCurrentChange() { GetListData() };
  async function GetListData() {
    const data = {
      "pageIndex": pagination.currentPage,
      "pageCount": pagination.pageSize,
      "taskId": route.query.taskId,
      "planId": route.query.planId,
    };
    const res = await ProFixPriceGetInsureList(data);
    if (res.success) {
      dataList.value = res.data?.items;
      pagination.total = res.data?.totalCount;
      loading.value = false;
    } else {
      ElMessage.error(res.msg);
      setTimeout(() => { loading.value = false; }, 1500)
    }
  }

  onMounted(() => {
    GetListData()
  })
  return {
    columns,
    dataList,
    loading,
    carmodel,
    pagination,
    btnloading,
    car_insurance_item,
    close_from,
    stubim_from,
    SendData,
    handleSizeChange,
    handleCurrentChange
  }
}
